/*
 * Copyright (C) 2020 Agrui
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef IPC_CHANNEL_H
#define IPC_CHANNEL_H

#include <stdint.h>
#include "IpcType.h"

#define CHANNEL_STATE_CONNECTED         0
#define CHANNEL_STATE_DISCONNECTED      1



#ifdef __cplusplus
extern "C" {
#endif

typedef void (*onDataReceiveCallback)(void* callbackData, int id, void* data, int dataLen);
typedef void (*onChannelStateChangedCallback)(void* callbackData, int id, int state);

typedef void (*registerChannelDataCallback)(onDataReceiveCallback dataReceiveCallback);
typedef void (*unregisterChannelDataCallback)();
typedef void (*sendChannelDataCallback)(void* channelHandle, IpcDataHeadInfo* header, void* data, int dataLen);
typedef void (*sendDataHandle)(void* channelHandle, void* data, int dataLen);

typedef struct {
    int channelId;
    int channelSize;

    sendDataHandle sendData;
    sendChannelDataCallback sendDataRequest;
    onDataReceiveCallback dataReceiveCallback;
    void*                         dataReceiveCallbackData;
    onChannelStateChangedCallback stateChangedCallback;
    void*                         stateCallbackData;
    registerChannelDataCallback registerDataCallback;
    unregisterChannelDataCallback unregisterDataCallback;
} IpcChannel;


IpcChannel* newIpcChannel(int id);
void initIpcChannel(int id, IpcChannel* channel);
void sendChannelAsyncData(IpcChannel* channel, IpcDataHeadInfo* header, void* data, int dataLen);
void sendChannelData(IpcChannel* channel, void* data, int dataLen);
void onIpcChannelDataReceive(int id, void* data, int dataLen);

void registerChannelReceiveCallback(IpcChannel* channel, onDataReceiveCallback callback, void* callbackData);
void unregisterChannelReceiveCallback(IpcChannel* channel);
void registerChannelStateChanged(IpcChannel* channel, onChannelStateChangedCallback callback, void* callbackData);
void unregisterChannelStateChanged(IpcChannel* channel);


void deleteIpcChannel(IpcChannel* channel);

#ifdef __cplusplus
}
#endif

#endif

